﻿using PMP.Core.Entities;
using PMP.Core.Extensions;
using PMP.Core.SqlSugar;

namespace PMP.Application.Apis;

/// <summary>
/// 消息管理
/// </summary>
public class NotificationApi : IDynamicApiController
{
    /// <summary>
    /// 获取通知
    /// </summary>
    /// <param name="isNotRead">是否未读：ture：获取未读消息；false：获取所有消息</param>
    /// <returns></returns>
    public async Task<List<TbNotification>> GetList([FromForm] bool isNotRead)
    {
        var list = await DbContext.Instance.Queryable<TbNotification>()
            .Where(x => x.TargetUserId == App.User.GetUserId())
            .WhereIF(isNotRead == true, x => x.IsDeal == false)
            .OrderBy(x => x.Id, OrderByType.Desc)
            .ToListAsync();
        return list;
    }

    /// <summary>
    /// 处理消息
    /// </summary>
    /// <param name="id">消息id</param>
    public async Task Read(int id)
    {
        await DbContext.Instance.Updateable<TbNotification>()
            .Where(a => a.Id == id)
            .SetColumns(a => a.IsDeal == true)
            .ExecuteCommandAsync();
    }
}